package com.ym.controller;

import com.ym.bean.User;
import com.ym.service.UserService;
import com.ym.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 用户接口controller
 *
 * @auther gilgamesh_ym
 * @date 2021/3/17 19:59
 **/
@RestController
@RequestMapping("api/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 新增用户
     *
     * @param user 用户
     * @return 新增结果
     * @author Gilgamesh_ym
     * @date 2021/3/17 20:57
     **/
    @PostMapping("insertUser")
    public R<Integer> insertUser(@RequestBody User user) {
        return R.ok(userService.insertUser(user));
    }

    /**
     * 根据用户主键查询用户
     *
     * @param id 用户主键
     * @return 查询结果
     * @author Gilgamesh_ym
     * @date 2021/3/17 20:56
     **/
    @GetMapping("{id}")
    public R<User> getUser(@PathVariable Integer id) {
        return R.ok(userService.get(id));
    }

    /**
     * 高级查询用户
     *
     * @param id       用户id
     * @param userId   用户userId
     * @param password 密码
     * @param userName 用户名
     * @return 结果集
     * @author Gilgamesh_ym
     * @date 2021/3/17 20:55
     **/
    @GetMapping("list")
    public R<List<User>> listUsers(@RequestParam(value = "id", required = false) Integer id,
                                   @RequestParam(value = "userId", required = false) String userId,
                                   @RequestParam(value = "userName", required = false) String userName,
                                   @RequestParam(value = "password", required = false) String password) {
        User user = new User();
        user.setUserId(userId);
        user.setUserName(userName);
        user.setPassword(password);
        user.setId(id);
        return R.ok(userService.list(user));
    }

    /**
     * 根据id修改对象
     *
     * @param user 用户对象
     * @return 修改返回结果
     * @author Gilgamesh_ym
     * @date 2021/3/17 20:55
     **/
    @PutMapping("updateById")
    public R<Integer> updateUserById(@RequestBody User user) {
        return R.ok(userService.updateById(user));
    }

    /**
     * 根据id删除用户
     *
     * @param id 用户表主键id
     * @return int
     * @author Gilgamesh_ym
     * @date 2021/3/17 20:53
     **/
    @DeleteMapping("{id}")
    public R<Integer> deleteById(@PathVariable Integer id) {
        return R.ok(userService.deleteById(id));
    }

}
